package com.mths.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.ImageButton;
import com.mths.client.component.Label;
import com.mths.client.constant.DirectorType;
import com.mths.client.dto.DirectorDTO;
import com.mths.client.page.CompanyRegister;
import com.mths.client.page.DirectorRegister;
import com.mths.client.util.ClientCacheUtil;

public class DirectorWidget extends Composite {

	private static DirectorWidgetUiBinder uiBinder = GWT.create(DirectorWidgetUiBinder.class);

	interface DirectorWidgetUiBinder extends UiBinder<Widget, DirectorWidget> {
	}

	@UiField
	Label mersisNo, tradeName, companyCenter, name, surname, title;

	@UiField
	FlowPanel corpDirectorPanel, realDirectorPanel;

	@UiField
	ImageButton updateBtn, deleteBtn;

	private DirectorDTO directorDTO = new DirectorDTO();
	private CompanyRegister companyRegister;
	private Boolean readOnly = Boolean.TRUE;

	public DirectorWidget(DirectorDTO dto, Boolean readOnly) {
		initWidget(uiBinder.createAndBindUi(this));
		directorDTO = dto;
		setReadOnly(readOnly);
		;
		createEventHandler();
		changePageDirectorType();
		componentValueSet();
		authorizationControl();
	}

	private void authorizationControl() {
		// control user role, for anonymous user remove update button
		if (!readOnly && ClientCacheUtil.instance().getPrivilegeItemList() != null
				&& ClientCacheUtil.instance().getPrivilegeItemList().contains("CompanyRegister_SELECT")) {
			updateBtn.setVisible(true);
			deleteBtn.setVisible(true);
		} else {
			updateBtn.removeFromParent();
			deleteBtn.removeFromParent();
		}
	}

	private void componentValueSet() {
		if (corpDirectorPanel != null && corpDirectorPanel.isVisible()) {
			mersisNo.setFieldValue(directorDTO.getMersisNo());
			tradeName.setFieldValue(directorDTO.getTradeName());
			companyCenter.setFieldValue(directorDTO.getCompanyCenter());
		} else {
			title.setFieldValue(directorDTO.getTitle());
		}
		name.setFieldValue(directorDTO.getName());
		surname.setFieldValue(directorDTO.getSurname());
	}

	public void changePageDirectorType() {
		if (directorDTO != null && directorDTO.getDirectorType().equals(DirectorType.REAL.toString())) {
			corpDirectorPanel.setVisible(false);
			realDirectorPanel.setVisible(true);
		} else {
			corpDirectorPanel.setVisible(true);
			realDirectorPanel.setVisible(false);
		}
	}

	private void createEventHandler() {
		updateBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				openDirectorPopup();
			}
		});

		deleteBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				companyRegister.deleteDirector(directorDTO);
				removeFromParent();
			}
		});
	}

	private void openDirectorPopup() {
		DirectorRegister directorRegister = companyRegister.openDirectorCreatePopup();
		directorRegister.openForUpdate(directorDTO);
	}

	public CompanyRegister getCompanyRegister() {
		return companyRegister;
	}

	public void setCompanyRegister(CompanyRegister companyRegister) {
		this.companyRegister = companyRegister;
	}

	public Boolean getReadOnly() {
		return readOnly;
	}

	public void setReadOnly(Boolean readOnly) {
		this.readOnly = readOnly;
	}
}
